/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package indexer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.String;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.omg.CORBA.Environment;

/**
 *
 * @author Claudiu
 */
class ReverseIndex {

    private ArrayList<wordEntry> entryList;
    private ArrayList<Integer> documents;
    //private Hashtable<String,Integer> termList;
    private ArrayList<String> termlistS;
    private ArrayList<Integer> termlistI;
    int wordCount;

    /**
     * @return the entryList
     */
    public ReverseIndex() {
        entryList = new ArrayList<wordEntry>();
        documents = new ArrayList<Integer>();
        termlistS = new ArrayList<String>();
        termlistI = new ArrayList<Integer>();
        wordCount = 0;
    }

    public ArrayList<wordEntry> getEntryList() {
        return entryList;
    }

    /**
     * @param entryList the entryList to set
     */
    public void setEntryList(ArrayList<wordEntry> entryList) {
        this.entryList = entryList;
    }

    /**
     * @return the documents
     */
    public ArrayList<Integer> getDocuments() {
        return documents;
    }

    /**
     * @param documents the documents to set
     */
    public void setDocuments(ArrayList<Integer> documents) {
        this.documents = documents;
    }

    void calculateWordValue(String word) {
        termlistS.add(word.trim().toLowerCase());
        termlistI.add(wordCount);
        wordCount++;

    }

    void parseDirectIndexFile(String filename, int docID) {
        try {
            ArrayList<String> words = new ArrayList<String>();
            FileReader reader = new FileReader(filename);
            BufferedReader input = new BufferedReader(reader);
            documents.add(docID);
            String line = input.readLine();
            //      System.out.println(line);
            while (line != null) {
                //System.out.println("in while");
                //System.out.println("line read : "+line);
                words = ProcessLine(line, docID);
                line = input.readLine();

            }
        } catch (Exception ex) {
        }
    }

    ArrayList<String> ProcessLine(String line, int docid) {
        ArrayList<String> words = new ArrayList<String>();
        String[] words_ = line.split(",");
        for (int i = 0; i < words_.length; i++) {

            Integer ax = termlistS.indexOf(words_[i].trim().toLowerCase());

            if (ax > -1) {
                Integer a = termlistI.get(ax);//aici era -1
                entryList.get(a).foundInDocument(docid);
            } else {
                //    System.out.println("in else branch "+ i + " ");
                calculateWordValue(words_[i].trim().toLowerCase());
                entryList.add(new wordEntry(/*termList.get(wordCount) */words_[i].trim().toLowerCase().hashCode(), docid));
            }
        }
        return words;
    }

    public void saveReverseIndexToFile(String filename) {
        FileWriter fstream = null;
        FileWriter fstreamCode = null;
        try {
            fstream = new FileWriter(filename);
            BufferedWriter file = new BufferedWriter(fstream);
            fstreamCode = new FileWriter(filename + " coding");
            BufferedWriter fileCode = new BufferedWriter(fstreamCode);
            //System.out.println(entryList.size());
            for (int i = 0; i < entryList.size(); i++) {

                file.append(entryList.get(i)._word + " :                    ");
                HashSet hs = new HashSet(entryList.get(i).documentList);
                ArrayList<Integer> aux = new ArrayList<Integer>(hs);
                for (int j = 0; j < aux.size(); j++) {
                    file.append(aux.get(j) + " , ");
                }
                file.append(" \n ");
            }
            file.close();
            fstream.close();
            for (int k = 0; k < termlistS.size() && k < termlistI.size(); k++) {
                fileCode.append(termlistS.get(k) + " : " + termlistS.get(k).hashCode() + " \n");
            }
            fileCode.close();
            fstreamCode.close();
        } catch (IOException ex) {
            Logger.getLogger(ReverseIndex.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fstream.close();
            } catch (IOException ex) {
                Logger.getLogger(ReverseIndex.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
